<?php

class CWebApplication
{
	protected $site;
	protected $modules;
	protected $assets;
	protected $upload;
	protected $layout;
	protected $theme;
	protected $routes;
	protected $cache;
	protected $runtime;
	protected $urlhandler;
	protected $urlhandlerclass;
	
	public function __construct($config=null)
	{	  
		$this->routes=new CMap();
		if(is_string($config))
		{	 
			$config=require($config);
			if(isset($config['site']))			
			$this->site=$config['site'];
			if(isset($config['modules']))	
			$this->modules=$config['modules'];
			if(isset($config['static']))	
			$this->assets=$config['static'];
			if(isset($config['upload']))	
			$this->upload=$config['upload'];
			if(isset($config['layout']))	
			$this->layout=$config['layout'];
			if(isset($config['theme']))	
			$this->theme=$config['theme'];
			if(isset($config['cache']))	
			$this->cache=$config['cache'];
			if(isset($config['runtime']))	
			$this->runtime=$config['runtime'];
			if(isset($config['urlhandler']))
			$this->urlhandler=$config['urlhandler'];
			if(isset($config['urlhandlerclass']))
			$this->urlhandlerclass=$config['urlhandlerclass'];
			if(isset($config['dbconfig']))
			$this->initDB($config['dbconfig']);
			if(isset($config['ormunits']))
			$this->addIncludeOrmUnitsClassPath($config['ormunits']);
			$this->setRoutes($config);
			unset($config);
		}
	}
	
	protected function initDB($dbconfig)
	{	
		
		 R::setup('mysql:host='
			.$dbconfig['host']
			.';dbname='
			.$dbconfig['database']
			,
			$dbconfig['username']
			,
			$dbconfig['password']
			);
		// R::freeze( true ); //will freeze redbeanphp
		
	}
	public function addIncludeOrmUnitsClassPath($compClssPath)
	{
			Deepr::addIncludePath($compClssPath);
			Deepr::addIncludePath($compClssPath.DIRECTORY_SEPARATOR.'dao');
			Deepr::addIncludePath($compClssPath.DIRECTORY_SEPARATOR.'model');
	}
	protected function setRoutes($config)
	{
		foreach ($this->modules as $curModule=>$modulepath)
		{			
			$defaultmap= new CRouteMapper
					(
						$this->site, 
						$curModule, 
						$this->modules[$curModule],
						null, 
						null,
						null,
						null,
						null,
						null,
						$this->layout,
						$this->theme,
						$this->upload,
						$this->assets,
						$this->cache,
						$this->runtime
					);
					$this->routes->add('default-'.$curModule,$defaultmap);
			if(!file_exists ($modulepath.DIRECTORY_SEPARATOR.'config'.
						DIRECTORY_SEPARATOR.'comp.inc.php'))
			 {
				return;
			 }		
			$components=include($modulepath.DIRECTORY_SEPARATOR.'config'.
						DIRECTORY_SEPARATOR.'comp.inc.php');			
			foreach ($components as $component)
			{
				$curComponentId=$component['id'];
				$routemap=$component['routemap'];
				
				foreach ($routemap as $route)
				{
					$curRouteIdentifier=$route['routeIdentifier'];
					$curController=$route['controller'];
					$curMethod=$route['method'];
					$curInterceptors=$route['interceptors'];								
					$map= new CRouteMapper
					(
						$this->site, 
						$curModule, 
						$this->modules[$curModule],
						$curComponentId, 
						$curRouteIdentifier,
						$curController,
						$this->modules[$curModule].
							DIRECTORY_SEPARATOR.'components'.
							DIRECTORY_SEPARATOR.$curComponentId.
							DIRECTORY_SEPARATOR.'controllers',
						$curMethod,
						$curInterceptors,
						$this->layout,
						$this->theme,
						$this->upload,
						$this->assets,
						$this->cache,
						$this->runtime
					);
					$this->routes->add($curRouteIdentifier,$map);
				}				
			}			
		}		
	}
	
	protected function processRequest()
	{		
		$data=new CMap();
		$data->add('CurrentApplication',$this);
		$requestHandler=$this->getRequestHandler();
		$requestURI=$requestHandler->getRequestURI();
		if(!empty($this->urlhandler))
		{
			 if(file_exists ($this->urlhandler))
			 {
				Deepr::autoloadByFullPath($this->urlhandler);
				$urlhandler =Deepr::createInstance($this->urlhandlerclass);		
				$urlhandler->init();
			 }	
		}
		else
		$urlhandler=Deepr::createInstance('CBaseURLHandler');		
		$urlhandler->setRoutes($this->routes);
		$route=$urlhandler->findRoute($requestURI);
		if(isset($route))
				$requestHandler->dispatchRequest($route,$data);
			else
				echo 'Page not found.Please contact to your administrator';	
		unset($urlhandler);		
	}
	
	protected function getRequestHandler($config=null)
	{
		return Deepr::defaultRequestHandler('CRequestHandler');
	}

	public function run()
	{	
		$this->processRequest();
		unset($this);
	}
	
	public function getSite()
	{
		return $this->site;
	}
	
	public function getLayout()
	{
		return $this->layout;
	}
	
	public function getTheme()
	{
		return $this->theme;
	}
}
